//默认房间
var defaultRoom = function(matchServer,app,uidArr,roomId) {
	this.app = app
	this.channelService = this.app.get('channelService')
	this.matchServer = matchServer
	this.uidArr = uidArr
	this.createTime = Date.now()
	this.roomData = {}
	this.userState = {}
	this.roomId = roomId
	for(var i = 0;i < this.uidArr.length;i++){
		this.userState[this.uidArr[i]] = {online : true}
	}
}
defaultRoom.prototype.init = function() {
	let notify = {
		type : "matchSuccess",
		roomInfo : {
			uidArr : this.uidArr,
			roomData : this.roomData,
			createTime : this.createTime
		}
	}
	this.sendToRoom(notify)
}
//关闭房间
defaultRoom.prototype.closeRoom = function() {
	var notify = {
		type : "finishRoom"
	}
	this.sendToRoom(notify)
}
//发送给玩家
defaultRoom.prototype.sendToUser = function(uid,notify) {
	this.channelService.pushMessageByUids('onMessage', notify, [{
      uid: uid,
      sid: this.app.serverId
    }])
}
//发送到房间
defaultRoom.prototype.sendToRoom = function(notify) {
	var uids = []
	for(var i = 0;i < this.uidArr.length;i++){
		uids.push({uid : this.uidArr[i],sid : this.app.serverId})
	}
	this.channelService.pushMessageByUids('onMessage', notify, uids)
}
//玩家掉线
defaultRoom.prototype.userleave = function(uid) {
	this.userState[uid].online = false
	var notify = {
		type : "disconnect",
		uid : uid
	}
	this.sendToRoom(notify)
}
//玩家重连
defaultRoom.prototype.userLogin = function(uid) {
	this.userState[uid].online = true
	var notify = {
		type : "reconnection",
		uid : uid,
		userState : this.userState
	}
	this.sendToRoom(notify)
}
//保存房间数据
defaultRoom.prototype.saveRoomData = function(key,value) {
	this.roomData[key] = value
	var notify = {
		"type" : "saveRoomData",
		"key" : key,
		"value" : value
	}
	this.sendToRoom(notify)
}
//获取房间数据
defaultRoom.prototype.getRoomData = function() {
	return this.roomData
}
module.exports = {
	id : "defaultRoom",
	func : defaultRoom,
	scope : "prototype",
	init : "init",
	args : [{
		name : "matchServer",
		type : "Object"
	},{
		name : "app",
		type : "Object"
	},{
		name : "uidArr",
		type : "Object"
	},{
		name : "roomId",
		type : "String"
	}]
}